#include "sqlite.hpp"
#include <assert.h>

// 回调函数
int select_callback(void* arg, int col_count, char** result, char** fields_name)
{
    std::vector<std::string> *array = static_cast<std::vector<std::string> *>(arg);
    array->push_back(result[0]);
    return 0; // 必须要返回
}

int main()
{
    // 1. 创建/打开一个数据库文件
    SqliteHelper sqher("./test.db");
    assert(sqher.open());
    // 2. 创建一个表 通讯录 telephone 姓名 关系 电话
    // const char* tel = "create table if not exists telephone(name varchar(32), unsigned int age, tel char(20));";
    // assert(sqher.exec(tel, nullptr, nullptr));
    // 3. 执行对应的增删查改操作

    // 增加
    // const char* insert_sql = "insert into telephone values('欧阳', 20, '123456789'), ('牛马', 21, 789456123), ('明东', 20, 147258369);";
    // assert(sqher.exec(insert_sql, nullptr, nullptr));

    // 更新
    // const char* update_sql = "update telephone set name='欧阳涟耀' where name='欧阳' ;";
    // assert(sqher.exec(update_sql, nullptr, nullptr));

    // 删除
    // const char* delete_sql = "delete from telephone where name='欧阳涟耀' ;";
    // assert(sqher.exec(delete_sql, nullptr, nullptr));

    // 查询
    std::vector<std::string> array;
    const char* select_sql = "select name from telephone;";
    assert(sqher.exec(select_sql, select_callback, &array));
    for(auto &name : array)
    {
        std::cout << name << std::endl;
    }

    // 4. 关闭对应的数据库文件
    sqher.close();
    return 0;
}